package com.meko.tms.core.context;


import java.util.Date;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @ClassName     UserContextHolder
 * @Description   实现用户信息保存的工具类
 * @Author        meko
 * @Date       2022/01/12 上午 10:49
 * @version:      1.0
 */
public class UserContextHolder {

    private static ThreadLocal<User> userInfoHolder = new ThreadLocal<>();

    public static void setCurrentUser(User user) {
        if(userInfoHolder.get() == null){
            userInfoHolder.set(user);//就会和当前的线程进行一一的绑定
        }
    }

    //获得当前的用户
    public static User getCurrentUser(){
        return userInfoHolder.get();
    }

    //对ThreadLocal进行清除。因为如果不清除的话，可能会造成内存泄露
    public static void removeCurrentUser(){
        userInfoHolder.remove();
    }


    @Data
    @NoArgsConstructor
    public static class User {
        private String uuid;
        private Date lastLogin;
    }
}
